package com.jnu.ilearn.bean;
import android.content.Context;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBank {
    public static final String DATA_NAME = "data";
    private final Context context;
    private List<PointRecord> PointRecordList =new ArrayList<>();
    private User user=new User( "account", "password", "alias","identity",3);
    public DataBank(Context context) {
        this.context=context;
    }

    public void loadData() {
        ObjectInputStream ois=null;
        try{
            ois = new ObjectInputStream(context.openFileInput(DATA_NAME));
            PointRecordList =(ArrayList<PointRecord>) ois.readObject();
            user=(User) ois.readObject();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public List<PointRecord> getPointRecordList (){
        return PointRecordList;
    }
    public User getUser(){
        return user;
    }
    public void saveData() {
        ObjectOutputStream oos=null;
        try{
            oos = new ObjectOutputStream(context.openFileOutput(DATA_NAME,Context.MODE_PRIVATE));
            oos.writeObject(PointRecordList);
            oos.writeObject(user);
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
